import express from "express"
import { User } from "../models/user.js"
import { Note } from '../models/note.js'
import notesController from "../controllers/notesController.js"
import userControllers from "../controllers/userControllers.js"
import {
  existedUserWithName, allUsers, checkUser, addNoteByUser, removeNoteByID,
  updateUserVisits, findAllNotesByUser, modifyNoteByID, findNotesByKeyword
} from "../models/db_apis.js"

const router = express.Router()

router.get('/', (req, res) => {
  res.render("index")
})



router.get("/about", (req, res) => {
  res.render("about")
})
// 注册start
router.get("/register", async (req, res, next) => {
  const users = await allUsers()
  req.session.usersCount = users.length
  res.render("register")
})
router.post("/register", userControllers.register)

// 登录start
router.get("/login", async (req, res, next) => {
  if (req.session.user_name) {
    return res.render("index", { username: req.session.user_name })
  }
  res.render("login")
})

router.post("/login", userControllers.login)

router.get("/logout", (req, res) => {
  req.session.destroy(err => {
    if (err) {
      return res.redirect('/')
    }
    res.render("index", { message: "已登出!" })
  })
})

// 文本列表start
router.get("/list-all-notes", notesController.getNotesList)
// 不建议使用get, title为空时, 地址栏会多出?title=, 若u获取直接post方法 参数放在请求体中则忽略不计
router.post("/list-all-notes", notesController.getNotesList)
// 新增
router.get("/add-note", (req, res) => {
  if (!req.session.user || Object.keys(req.session.user).length === 0) {
    res.render("login")
  } else {
    res.render("add-note")
  }
})

router.post("/add-note", notesController.addNotes)

// 删除
router.post("/delete-note", notesController.delNotes)
// 更新笔记
router.get('/upd-note', (req, res) => {
  const noteId = req.query.id
  notesController.getNoteDetail(req, res, noteId)
})
router.post("/upd-note", notesController.updNotes)
router.post("/collect-note", notesController.collectnotes)

// 获取我的收藏
router.get("/mine-collect", notesController.myCollectNoteList)
// 修改密码
router.get('/reset', (req, res) => {
  const id = req.session.user.id
  userControllers.getUserInfo(req, res, id)
})
router.post('/reset', userControllers.updUser)
// router.get("/find-note", (req, res) => {
//   if (!req.session.regid) {
//     res.render("login")
//   } else {
//     res.render("find-note")
//   }
// })

// router.post("/find-note", async (req, res, next) => {
//   if (req.session.user_name === undefined) {
//     res.render("login")
//   } else {
//     const { title } = req.body
//     if (!title) {
//       return res.render("find-note", { titleError: true })
//     }
//     const notesData = await findNotesByKeyword(req.session.user_name, title)
//     res.render("found-note", { count: notesData.length, notes: notesData.map(note => note.toObject()) })
//   }
// })

// router.get("/delete-note", async (req, res, next) => {
//   if (req.session.user_name === undefined) {
//     res.render("login")
//   } else {
//     const notesData = await findAllNotesByUser(req.session.user_name)
//     const notes = notesData.notes
//     res.render("delete-note", { count: notes.length, notes: notes.map(note => note.toObject()) })
//   }
// })

// router.get("/modify-note", async (req, res, next) => {
//   if (req.session.user_name === undefined) {
//     res.render("login")
//   } else {
//     const notesData = await findAllNotesByUser(req.session.user_name)
//     const notes = notesData.notes
//     res.render("modify-note", { count: notes.length, notes: notes.map(note => note.toObject()) })
//   }
// })


// router.post("/modify-note", async (req, res, next) => {
//   if (req.session.user_name === undefined) {
//     res.render("login")
//   } else {
//     await modifyNoteByID(req.session.user_name, req.body.noteId, req.body.title, req.body.content)
//     const notesData = await findAllNotesByUser(req.session.user_name)
//     const notes = notesData.notes
//     res.render("modify-note", { count: notes.length, notes: notes.map(note => note.toObject()) })
//   }
// })

// router.post("/modified-note", async (req, res, next) => {
//   if (req.session.user_name === undefined) {
//     res.render("login")
//   } else {
//     res.render("modified-note", { title: req.body.title, content: req.body.content, _id: req.body.noteId })
//   }
// })


export { router }
